<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>AdWords::AuthToken</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
    <script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>     
    <div class="banner">
        <h1>
            <span class="type">Module</span> 
            AdWords::AuthToken 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/lib/adwords4r/authtoken_rb.html">lib/adwords4r/authtoken.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    
    <div class="description">
        <p>
Module providing the mechanism to obtain auth tokens for logging in to the
<a href="../AdWords.html">AdWords</a> <a href="API.html">API</a> (>=
v200902).
</p>

    </div>
    

    

    
    

    
    
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
    
        <dt>G</dt>
        <dd>
            <ul>
                
                <li><a href="#M000026">get_token</a></li>
                
            </ul>
        </dd>
    
    </dl>
    

    

    

    

    
    <div class="sectiontitle">Constants</div>
    <table border='0' cellpadding='5'>
        
        <tr valign='top'>
            <td class="attr-name">ACCOUNT_TYPE</td>
            <td>=</td>
            <td class="attr-value">'GOOGLE'</td>
        </tr>
        
        
        <tr valign='top'>
            <td class="attr-name">AUTH_PATH</td>
            <td>=</td>
            <td class="attr-value">'/accounts/ClientLogin'</td>
        </tr>
        
        
        <tr valign='top'>
            <td class="attr-name">SERVICE</td>
            <td>=</td>
            <td class="attr-value">'adwords'</td>
        </tr>
        
        
    </table>
    

    

    
            <div class="sectiontitle">Class Public methods</div>
            
            <div class="method">
                <div class="title" id="M000026">
                    
                    <a name="M000026"></a><b>get_token</b>(email, password, hostname, port, use_ssl)
                    
                </div>
                
                <div class="description">
                  <p>
Retrieve authentication token for logging in to the <a
href="../AdWords.html">AdWords</a> <a href="API.html">API</a>.
</p>
<p>
Args:
</p>
<ul>
<li>email: the email address for the account being accessed

</li>
<li>password: the password for the account being accessed

</li>
<li>hostname: the hostname to connect to

</li>
<li>port: the port to connect to

</li>
<li>use_ssl: boolean indicating whether to use SSL or not

</li>
</ul>
<p>
Returns: The auth token for the account (as a string).
</p>
<p>
Raises: <a href="Error/AuthError.html">AdWords::Error::AuthError</a> if
authentication fails.
</p>

                </div>
                
                
                
                
                <div class="sourcecode">
                    <p class="source-link">
                        Source: <a href="javascript:toggleSource('M000026_source')" id="l_M000026_source">show</a>
                        
                    </p>
                    <div id="M000026_source" class="dyn-source">
                        <pre><span class="ruby-comment cmt"># File lib/adwords4r/authtoken.rb, line 53</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">get_token</span>(<span class="ruby-identifier">email</span>, <span class="ruby-identifier">password</span>, <span class="ruby-identifier">hostname</span>, <span class="ruby-identifier">port</span>, <span class="ruby-identifier">use_ssl</span>)
      <span class="ruby-identifier">email</span> = <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">email</span>)
      <span class="ruby-identifier">password</span> = <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">password</span>)

      <span class="ruby-identifier">http_client</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">hostname</span>, <span class="ruby-identifier">port</span>)
      <span class="ruby-identifier">http_client</span>.<span class="ruby-identifier">use_ssl</span> = <span class="ruby-identifier">use_ssl</span>
      <span class="ruby-comment cmt"># Avoid annoying warning</span>
      <span class="ruby-identifier">http_client</span>.<span class="ruby-identifier">verify_mode</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">VERIFY_NONE</span>

      <span class="ruby-identifier">data</span> = <span class="ruby-node">&quot;accountType=#{ACCOUNT_TYPE}&amp;Email=#{email}&amp;Passwd=#{password}&quot;</span> <span class="ruby-operator">+</span>
        <span class="ruby-node">&quot;&amp;service=#{SERVICE}&quot;</span>
      <span class="ruby-identifier">headers</span> = {<span class="ruby-value str">'Content-Type'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'application/x-www-form-urlencoded'</span>}

      <span class="ruby-identifier">response</span> = <span class="ruby-identifier">http_client</span>.<span class="ruby-identifier">post</span>(<span class="ruby-constant">AUTH_PATH</span>, <span class="ruby-identifier">data</span>, <span class="ruby-identifier">headers</span>)

      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'200'</span>
        <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>[<span class="ruby-regexp re">/Auth=(.*)/</span>, <span class="ruby-value">1</span>]
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Error</span><span class="ruby-operator">::</span><span class="ruby-constant">AuthError</span>,
            <span class="ruby-value str">&quot;Login failed for email %s: %s (code %d)&quot;</span> <span class="ruby-operator">%</span>
                [<span class="ruby-constant">CGI</span>.<span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">email</span>), <span class="ruby-identifier">response</span>.<span class="ruby-identifier">message</span>, <span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span>]
      <span class="ruby-keyword kw">end</span>
    <span class="ruby-keyword kw">end</span></pre>
                    </div>
                </div>
                
            </div>
            
</div>
    </div>
  </body>
</html>    